package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.service.goods.SkuService;
import com.qingcheng.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wuchong
 * @title: AdminController
 * @projectName qingcheng_parent
 * @description: TODO
 * @date 2019.07.10下午 06:40
 */
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {


    private String getLoginUsername(){
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }


    @Reference
    private SkuService skuService;

    @GetMapping("/addToHistory")
    @CrossOrigin
    public Result addToHistory(String username, String skuId){

        skuService.addToHistory(username,skuId);
        return new Result();
    }

    @GetMapping("/getViewHistory")
    public List<Sku> getViewHistory(){
        String username = getLoginUsername();
        List<Sku> skuList=skuService.getViewHistory(username);
        return skuList;
    }

    @GetMapping("/deleHistory")
    public Result deleHistory(String skuId){
        String username = getLoginUsername();
        skuService.deleHistory(username,skuId);
        return new Result();
    }

    @GetMapping("/addToFav")
    @CrossOrigin
    public boolean addToFav(String username, String skuId){

        boolean flag=skuService.addToFav(username,skuId);
       return flag;
    }
    @GetMapping("/deleFav")
    public Result deleFav(String skuId){
        String username = getLoginUsername();
        skuService.deleFav(username,skuId);
        return new Result();
    }

    @GetMapping("/findSkuList")
    @CrossOrigin
    public List<Sku> findSkuList(){
        String username = getLoginUsername();
        return skuService.findSkuListFromFav(username);
    }
}
